package com.huawei.demo;

import com.huaweicloud.sdk.cfw.v1.CfwClient;
import com.huaweicloud.sdk.cfw.v1.model.ChangeIpsProtectModeUsingPostRequest;
import com.huaweicloud.sdk.cfw.v1.model.ChangeIpsProtectModeUsingPostResponse;
import com.huaweicloud.sdk.cfw.v1.model.ChangeIpsSwitchUsingPostRequest;
import com.huaweicloud.sdk.cfw.v1.model.ChangeIpsSwitchUsingPostResponse;
import com.huaweicloud.sdk.cfw.v1.model.ChangeProtectEipRequest;
import com.huaweicloud.sdk.cfw.v1.model.ChangeProtectEipResponse;
import com.huaweicloud.sdk.cfw.v1.model.EipOperateProtectReq;
import com.huaweicloud.sdk.cfw.v1.model.EipOperateProtectReqIpInfos;
import com.huaweicloud.sdk.cfw.v1.model.EipResource;
import com.huaweicloud.sdk.cfw.v1.model.IpsProtectDTO;
import com.huaweicloud.sdk.cfw.v1.model.IpsSwitchDTO;
import com.huaweicloud.sdk.cfw.v1.model.ListAttackLogsRequest;
import com.huaweicloud.sdk.cfw.v1.model.ListAttackLogsResponse;
import com.huaweicloud.sdk.cfw.v1.model.ListEipResourcesRequest;
import com.huaweicloud.sdk.cfw.v1.model.ListEipResourcesResponse;
import com.huaweicloud.sdk.cfw.v1.region.CfwRegion;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;

import java.util.ArrayList;
import java.util.List;

public class IpsRule {
    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
        BasicCredentials auth = new BasicCredentials().withAk(ak).withSk(sk);
        CfwClient client = CfwClient.newBuilder().withCredential(auth).withRegion(CfwRegion.valueOf("<REGION ID>")).build();

        try {
            /* 4.4.1 通过查询防护eip列表查询到一条防护eip的地址 */
            EipResource eipResource = queryEip(client);
            String publicEIp = eipResource.getPublicIp();
            String id = eipResource.getId();
            System.out.println(publicEIp);
            System.out.println(id);

            /* 4.4.2 开启eip防护 */
            changeEipProtectionStatus(client,id,publicEIp);

            /* 4.4.3 开启ips防护 */
            changeIpsStatus(client);

            /* 4.4.4 设置ips防护模式为严格 */
            changeIpsMode(client);

            /* 4.4.5 查询攻击日志,获得攻击日志 */
            queryAttackLogs(client,publicEIp);
        } catch (ConnectionException e) {
            System.out.println(e.getMessage());
        } catch (RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static void queryAttackLogs(CfwClient client,String publicEIp) {
        ListAttackLogsRequest listAttackLogsRequest = new ListAttackLogsRequest();
        listAttackLogsRequest.setDstIp(publicEIp);
        listAttackLogsRequest.setFwInstanceId("<YOUR FirewallInstanceId>");
        listAttackLogsRequest.setStartTime(1670427589817L);
        listAttackLogsRequest.setEndTime(1670431189817L);
        listAttackLogsRequest.setLimit(10);
        ListAttackLogsResponse listAttackLogsResponse = client.listAttackLogs(listAttackLogsRequest);
        System.out.println(listAttackLogsResponse.toString());
    }

    private static void changeIpsMode(CfwClient client) {
        IpsProtectDTO ipsProtectDTO = new IpsProtectDTO();
        ipsProtectDTO.setObjectId("<YOUR ObjectId>");
        ipsProtectDTO.setMode(1);
        ChangeIpsProtectModeUsingPostRequest changeIpsProtectModeUsingPostRequest = new ChangeIpsProtectModeUsingPostRequest();
        changeIpsProtectModeUsingPostRequest.setBody(ipsProtectDTO);
        ChangeIpsProtectModeUsingPostResponse changeIpsProtectModeUsingPostResponse = client.changeIpsProtectModeUsingPost(changeIpsProtectModeUsingPostRequest);
        System.out.println(changeIpsProtectModeUsingPostResponse.toString());
    }

    private static void changeIpsStatus(CfwClient client) {
        IpsSwitchDTO ipsSwitchDTO = new IpsSwitchDTO();
        ipsSwitchDTO.setIpsType(IpsSwitchDTO.IpsTypeEnum.NUMBER_1);
        ipsSwitchDTO.setObjectId("<YOUR ObjectId>");
        ipsSwitchDTO.setStatus(1);
        ChangeIpsSwitchUsingPostRequest changeIpsSwitchUsingPostRequest = new ChangeIpsSwitchUsingPostRequest();
        changeIpsSwitchUsingPostRequest.setBody(ipsSwitchDTO);
        ChangeIpsSwitchUsingPostResponse changeIpsSwitchUsingPostResponse = client.changeIpsSwitchUsingPost(changeIpsSwitchUsingPostRequest);
        System.out.println(changeIpsSwitchUsingPostResponse.toString());
    }

    private static void changeEipProtectionStatus(CfwClient client,String id,String publicEIp) {
        ChangeProtectEipRequest changeProtectEipRequest = new ChangeProtectEipRequest();
        EipOperateProtectReq eipOperateProtectReq = new EipOperateProtectReq();
        eipOperateProtectReq.setObjectId("<YOUR ObjectId>");
        eipOperateProtectReq.setStatus(0);
        List<EipOperateProtectReqIpInfos> ipsInfos = new ArrayList<>();
        EipOperateProtectReqIpInfos eipOperateProtectReqIpInfos = new EipOperateProtectReqIpInfos();
        eipOperateProtectReqIpInfos.setId(id);
        eipOperateProtectReqIpInfos.setPublicIp(publicEIp);
        ipsInfos.add(eipOperateProtectReqIpInfos);
        eipOperateProtectReq.setIpInfos(ipsInfos);
        changeProtectEipRequest.setBody(eipOperateProtectReq);
        ChangeProtectEipResponse changeProtectEipResponse = client.changeProtectEip(changeProtectEipRequest);
        System.out.println(changeProtectEipResponse.toString());
    }

    private static EipResource queryEip(CfwClient client) {
        ListEipResourcesRequest listEipResourcesRequest = new ListEipResourcesRequest();
        listEipResourcesRequest.setObjectId("<YOUR ObjectId>");
        listEipResourcesRequest.setLimit(10);
        listEipResourcesRequest.setOffset(0);
        listEipResourcesRequest.setSync(ListEipResourcesRequest.SyncEnum.NUMBER_1);
        ListEipResourcesResponse listEipResourcesResponse = client.listEipResources(listEipResourcesRequest);
        EipResource eipResource = listEipResourcesResponse.getData().getRecords().get(0);
        return eipResource;
    }
}
